package com.yupi.springbootinit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.mapper.ChatRoomMapper;
import com.yupi.springbootinit.model.dto.chat.ChatRoomDto;
import com.yupi.springbootinit.model.dto.chat.ChatRoomQueryRequest;
import com.yupi.springbootinit.model.entity.ChatMessages;
import com.yupi.springbootinit.model.entity.ChatRoom;
import com.yupi.springbootinit.service.ChatMessageService;
import com.yupi.springbootinit.service.ChatRoomService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 尔特岛
* @description 针对表【chatroom(聊天房间表)】的数据库操作Service实现
* @createDate 2025-03-27 16:22:41
*/
@Service
public class ChatRoomServiceImpl extends ServiceImpl<ChatRoomMapper, ChatRoom> implements ChatRoomService {

    @Resource
    private ChatMessageService chatMessageService;

    @Override
    public Page<ChatRoomDto> getChatRoomPage(ChatRoomQueryRequest chatRoomQueryRequest, HttpServletRequest request) {
        if(chatRoomQueryRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long pageSize = chatRoomQueryRequest.getPageSize();
        long current = chatRoomQueryRequest.getCurrent();
        Page<ChatRoom> page = new Page<>(current,pageSize);
        Page<ChatRoomDto> pageDto = new Page<>(current,pageSize);

        LambdaQueryWrapper<ChatRoom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatRoom::getStatus,0);
        queryWrapper.orderByDesc(ChatRoom::getCreateTime);
        queryWrapper.eq(ChatRoom::getUserId,chatRoomQueryRequest.getUserId());
        page = this.page(page, queryWrapper);

        BeanUtil.copyProperties(page,pageDto,"records");

        //手动处理 pageDto 的records数据
        List<ChatRoom> records = page.getRecords();
        List<ChatRoomDto> list = records.stream().map((item)->{
            ChatRoomDto chatRoomDto = new ChatRoomDto();
            //拷贝房间的基本信息，房间聊天记录另外存储
            BeanUtil.copyProperties(item,chatRoomDto);
            Long roomId = item.getId();
            //设置某个房间的全部聊天信息
            List<ChatMessages> chatMessageList = getChatMessageList(roomId.toString());
            chatRoomDto.setChatMessagesList(chatMessageList);
            return chatRoomDto;
        }).collect(Collectors.toList());

        pageDto.setRecords(list);

        return pageDto;
    }

    /**
     * 获取某个房间的全部聊天信息
     * @param roomId
     * @return
     */
    public List<ChatMessages> getChatMessageList(String roomId){
        LambdaQueryWrapper<ChatMessages> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatMessages::getRoomId,roomId);
        List<ChatMessages> list = chatMessageService.list(queryWrapper);
        if(list == null){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return list;
    }


    /**
     * 查询房间列表id
     * @param chatRoomQueryRequest
     * @param request
     * @return
     */
//    @Override
//    public Page<ChatRoom> getChatRoomPage(ChatRoomQueryRequest chatRoomQueryRequest, HttpServletRequest request) {
//        long pageSize = chatRoomQueryRequest.getPageSize();
//        long current = chatRoomQueryRequest.getCurrent();
//        Page<ChatRoom> chatRoomPage = new Page<>(current,pageSize);
//        LambdaQueryWrapper<ChatRoom> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ChatRoom::getStatus,0);
//        queryWrapper.eq(ChatRoom::getUserId,chatRoomQueryRequest.getUserId());
//        Page<ChatRoom> page = this.page(chatRoomPage, queryWrapper);
//        return page;
//    }
}




